<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProvinceModel
 *
 * @author eoe2005
 */
class ProvinceModel {
    /**
     *获取系统中的全部省份表
     */
    public function getAllProvince(){
        $db = getDB();
        Debug::obj($db);
        $list = $db->get_results(sprintf('SELECT Fid AS id,Fname AS provinceName,Ftype AS type FROM t_province'));
        if($list){
            return $list;
        }
        return array();
    }
    /**
     *根据省份获取城市的名字
     * @param type $pid
     * @return type
     */
    public function getCitysByProvince($pid){
        $db = getDB();
        $list = $db->get_results(sprintf('SELECT Fcid AS id,Fname AS cityName,Fpid AS provinceId FROM t_city WHERE Fpid=%d',$pid));
        if($list){
            return $list;
        }
        return array();
    }
    /**
     *获取全部的城市信息
     * @return type
     */
    public function getAllCity(){
        $db = getDB();
        $list = $db->get_results('SELECT Fcid AS id,Fname AS cityName,Fpid AS provinceId FROM t_city');
        if($list){
            return $list;
        }
        return array();
    }
    public function all(){
        $list = $this->getAllProvince();
        Debug::obj(getDB());
        $cs = $this->getAllCity();

        Debug::obj(getDB());
        foreach($list AS $k => $v){
            foreach($cs AS $key => $val){
                if($val['provinceId'] == $v['id']){
                    if(isset($v['citys'])){
                        $v['citys'][] = $val;
                    }else{
                        $v['citys'] = array($val);
                    }
                    unset($cs[$key]);
                }
            }
            $list[$k] = $v;
        }
        return $list;
    }
}

?>
